package freshmanspecial.mredrock.com.welcomefreshman.View;

import android.content.res.Resources;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import freshmanspecial.mredrock.com.welcomefreshman.Adapter.MyPagerAdapter;
import freshmanspecial.mredrock.com.welcomefreshman.Base.BaseActivity;

import freshmanspecial.mredrock.com.welcomefreshman.Fragment.DataFragmentThree;
import freshmanspecial.mredrock.com.welcomefreshman.Fragment.Stragegy_qqsearch_fragment;
import freshmanspecial.mredrock.com.welcomefreshman.Fragment.Stragegy_zhoubian_fragment;
import freshmanspecial.mredrock.com.welcomefreshman.Fragment.StrategyFragment;
import freshmanspecial.mredrock.com.welcomefreshman.Fragment.Strategy_qqgroup_fragment;
import freshmanspecial.mredrock.com.welcomefreshman.Fragment.Strategy_ruxue_Fragment;
import freshmanspecial.mredrock.com.welcomefreshman.Model.DataModel;
import freshmanspecial.mredrock.com.welcomefreshman.Presenter.StrategyPresenter;
import freshmanspecial.mredrock.com.welcomefreshman.R;

/**
 * Created by HP on 2017/8/6.
 */

public class StrategyView extends BaseActivity implements IStrategyView {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private MyPagerAdapter adapter;
    private List<Fragment> list;
    private TextView text_title;
    private ImageButton btn_backToMainView;
    private StrategyPresenter presenter;
    private  ArrayList<String> titleList1,contentList1,titleList2,contentList2,titleList3,contentList3;
    private String[] titles = {"校园环境","学生寝室","学校食堂","入学须知","QQ群","日常生活","周边美食","周末美景"};
    private int Meishi=2,MeiJing=3,Shenghuo=1;
    @Override
    protected void initData() {
        text_title = $(R.id.text_titlebar);
        text_title.setText("邮子攻略");
        titleList1 = new ArrayList<>();
        contentList1 = new ArrayList<>();
        titleList2 = new ArrayList<>();
        contentList2 = new ArrayList<>();
        titleList3 = new ArrayList<>();
        contentList3 = new ArrayList<>();
        btn_backToMainView = $(R.id.btn_back);
        presenter = new StrategyPresenter(this);
        viewPager = $(R.id.strategy_pager);
        tabLayout = $(R.id.strategy_tab);
        list = new ArrayList<>();
        list.add(new StrategyFragment(1));
        list.add(new StrategyFragment(2));
        list.add(new StrategyFragment(3));
        list.add(new Strategy_ruxue_Fragment());
        list.add(new Strategy_qqgroup_fragment());
        list.add(new Stragegy_zhoubian_fragment(6));
        list.add(new Stragegy_zhoubian_fragment(7));
        list.add(new Stragegy_zhoubian_fragment(8));
        adapter = new MyPagerAdapter(getSupportFragmentManager());
        adapter.setTitles(titles);
        adapter.setFragments(list);
        viewPager.setOffscreenPageLimit(2);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(tabLayout,27,27);
            }
        });
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        setBackToMainView();
    }


    public void setIndicator(TabLayout tabs,int leftDip,int rightDip){
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }

    @Override
    protected int getContentViewId() {
        return R.layout.special_2017_strategyview;
    }

    @Override
    public void setBackToMainView() {
        btn_backToMainView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                presenter.backToMainView();
            }
        });
    }
}
